<?php
/**
 * Created by PhpStorm.
 * User: xutao
 * Date: 2018/11/8
 * Time: 17:02
 */
//角色
namespace app\admin\model;
use think\Loader;
use think\Model;

class AuthGroup extends Model{

    protected $table = 'auth_group';
    protected $autoWriteTimestamp = 'datetime';

    //添加
    public function add($data){
        $AdminValidate  = Loader::validate('AuthgroupValidate');
        $check = $AdminValidate->check($data);
        if(!$check) return json(['code'=>1,'data'=>$AdminValidate->getError(),'msg'=>$AdminValidate->getError()]);
        //查询角色是否存在
        $is_cunzai = self::where('title',$data['title'])->find();
        if($is_cunzai) return json(['code'=>2,'data'=>'','msg'=>'该角色已存在']);
        $re = $this->allowField(true)->save($data);
        if($re) return json(['code'=>200,'data'=>'','msg'=>'添加成功']);
        return json(['code'=>0,'data'=>'','msg'=>'添加失败']);

    }
    //编辑
    public function edit($data){
        $AdminValidate  = Loader::validate('AuthgroupValidate');
        $check = $AdminValidate->check($data);
        if(!$check) return json(['code'=>1,'data'=>$AdminValidate->getError(),'msg'=>$AdminValidate->getError()]);
        //查询角色是否存在
        $is_cunzai = self::where('title',$data['title'])->where('id','<>',$data['id'])->find();
        if($is_cunzai) return json(['code'=>2,'data'=>'','msg'=>'该角色已存在']);
        $re = $this->allowField(true)->save($data,['id'=>$data['id']]);
        if($re) return json(['code'=>200,'data'=>'','msg'=>'更新成功']);
        return json(['code'=>0,'data'=>'','msg'=>'更新失败']);

    }


}